package org.example.serviceEdu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.example.serviceEdu.client.VodClient;
import org.example.serviceEdu.entity.EduVideo;
import org.example.serviceEdu.mapper.EduVideoMapper;
import org.example.serviceEdu.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author lining
 * @since 2023-10-25
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    @Autowired
    private VodClient vodClient;

    @Override
    public void removeVideoByCourseId(String courseId){
        //根据课程id查询课程中所有的视频id(视频id在小节表中,所以我们查小节表edu_video)
        ////①得到List<EduVideo>
        QueryWrapper<EduVideo> wrapperVideo=new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        wrapperVideo.select("video_source_id");
        List<EduVideo> eduVideoList=baseMapper.selectList(wrapperVideo);
        List<String> videoIds=new ArrayList<>();
        for(int i=0;i<eduVideoList.size();i++){
            EduVideo eduVideo=eduVideoList.get(i);
            String videoSourceId=eduVideo.getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId)){
                videoIds.add(videoSourceId);
            }
        }
        //2.远程调用:根据多个视频id删除阿里云中的视频
//判断:如果课程下没有一个视频,那就不用调用这个方法了
        if (videoIds.size()>0){
            vodClient.deleteBatch(videoIds);
        }

        //    id删除节
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }

}
